//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.andromeda.androbench2;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.os.Build.VERSION;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class MemoryStatus {
    static final int ERROR = -1;
    static final String ERROR_STR = "-1";
    static final String UNKNOWN = "unknown";

    public MemoryStatus() {
    }

    public static String formatSize(long var0) {
        long var6;
        int var2 = (var6 = var0 - 1024L) == 0L ? 0 : (var6 < 0L ? -1 : 1);
        String var3 = null;
        if (var2 >= 0) {
            var3 = "KiB";
            var0 /= 1024L;
            if (var0 >= 1024L) {
                var3 = "MiB";
                var0 /= 1024L;
            }
        }

        StringBuilder var4 = new StringBuilder(Long.toString(var0));

        for(int var5 = -3 + var4.length(); var5 > 0; var5 -= 3) {
            var4.insert(var5, ',');
        }

        if (var3 != null) {
            var4.append(var3);
        }

        return var4.toString();
    }

    public static long getAvailableInternalMemorySize() {
        if (isAvailableDataPath()) {
            StatFs var0 = new StatFs(Environment.getDataDirectory().getPath());
            return VERSION.SDK_INT < 18 ? (long)var0.getBlockSize() * (long)var0.getAvailableBlocks() : var0.getAvailableBytes();
        } else {
            return -1L;
        }
    }

    public static long getAvailableSDExternalMemorySize(Context var0) {
        String var1 = getSDExternalPath(var0);
        if (!var1.equals("unknown")) {
            StatFs var2 = new StatFs((new File(var1)).getPath());
            return VERSION.SDK_INT < 18 ? (long)var2.getBlockSize() * (long)var2.getAvailableBlocks() : var2.getAvailableBytes();
        } else {
            return -1L;
        }
    }

    public static String getFilesystem(String paramString)
    {
        try
        {
            String[] arrayOfString1 = { "/system/bin/sh", "-c", "cat /proc/mounts" };
            BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(arrayOfString1).getInputStream()));
            while (true)
            {
                String str1 = localBufferedReader.readLine();
                if (str1 == null)
                    break;
                String[] arrayOfString2 = str1.split(" ");
                if (!arrayOfString2[1].equals(paramString))
                    continue;
                String str2 = arrayOfString2[2];
                return str2;
            }
        }
        catch (IOException localIOException)
        {
        }
        return "unknown";
    }


    public static String getGovernor() {
        String var2;
        try {
            String[] var0 = new String[]{"/system/bin/sh", "-c", "cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"};
            var2 = (new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(var0).getInputStream()))).readLine();
        } catch (IOException var3) {
            return "unknown";
        }

        if (var2 != null) {
            return var2;
        } else {
            return "unknown";
        }
    }

    public static String getGovernor_IO_IS_BUSY() {
        String var3;
        try {
            String var1 = getGovernor();
            if (var1.equals("-")) {
                return "-1";
            }

            String[] var2 = new String[]{"/system/bin/sh", "-c", "cat /sys/devices/system/cpu/cpufreq/" + var1 + "/io_is_busy"};
            var3 = (new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(var2).getInputStream()))).readLine();
        } catch (IOException var4) {
            return "-1";
        }

        if (var3 != null) {
            return var3;
        } else {
            return "-1";
        }
    }

    public static String getInternalFilesystem() {
        return getFilesystem("/data");
    }

    public static String getInternalScheduler() {
        return getScheduler(GetInfo.getInternalDevName());
    }

    public static String getSDExternalFilesystem(Context var0) {
        return getFilesystem(getSDExternalPath(var0));
    }

    public static String getSDExternalPath(Context var0) {
        if (VERSION.SDK_INT >= 19) {
            File[] var7 = var0.getExternalFilesDirs((String)null);
            if (var7 != null && isExternalStorageWritable()) {
                if (var7.length > 1 && var7[0] != null) {
                    String var8 = var7[0].toString().replaceFirst(Environment.getExternalStorageDirectory().getAbsolutePath(), "");
                    if (var7[1] != null && var8 != null) {
                        return var7[1].toString().replaceFirst(var8, "");
                    }
                }

                return "unknown";
            } else {
                return "unknown";
            }
        } else {
            boolean var10001;
            BufferedReader var2;
            try {
                String[] var1 = new String[]{"/system/bin/sh", "-c", "cat /proc/mounts"};
                var2 = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(var1).getInputStream()));
            } catch (IOException var11) {
                var10001 = false;
                return "unknown";
            }

            while(true) {
                String var4;
                try {
                    var4 = var2.readLine();
                } catch (IOException var10) {
                    var10001 = false;
                    break;
                }

                if (var4 == null) {
                    break;
                }

                try {
                    String[] var5 = var4.split(" ");
                    if (var5[1].equals("/storage/extSdCard")) {
                        String var6 = var5[1];
                        return var6;
                    }
                } catch (Exception var9) {
                    var10001 = false;
                    break;
                }
            }

            return "unknown";
        }
    }

    public static String getSDExternalScheduler() {
        return getScheduler("mmcblk1");
    }

    public static String getScheduler(String var0) {
        boolean var10001;
        BufferedReader var3;
        try {
            String[] var2 = new String[]{"/system/bin/sh", "-c", "cat /sys/block/" + var0 + "/queue/scheduler"};
            var3 = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(var2).getInputStream()));
        } catch (IOException var11) {
            var10001 = false;
            return "unknown";
        }

        while(true) {
            String var4;
            try {
                var4 = var3.readLine();
            } catch (IOException var10) {
                var10001 = false;
                break;
            }

            if (var4 == null) {
                break;
            }

            String[] var5;
            try {
                var5 = var4.split(" ");
            } catch (Exception var9) {
                var10001 = false;
                break;
            }

            int var6 = 0;

            while(true) {
                try {
                    if (var6 >= var5.length) {
                        break;
                    }

                    if (var5[var6].charAt(0) == '[') {
                        String var7 = var5[var6].substring(1, -1 + var5[var6].length());
                        return var7;
                    }
                } catch (Exception var8) {
                    var10001 = false;
                    return "unknown";
                }

                ++var6;
            }
        }

        return "unknown";
    }

    public static long getTotalInternalMemorySize() {
        return getTotalMemorySize(Environment.getDataDirectory());
    }

    public static int getTotalMem() {
        try {
            String[] var0 = new String[]{"/system/bin/sh", "-c", "cat /proc/meminfo"};
            int var2 = Integer.parseInt((new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(var0).getInputStream()))).readLine().split(":")[1].split("kB")[0].trim());
            return var2;
        } catch (IOException var3) {
            return 0;
        }
    }

    private static long getTotalMemorySize(File var0) {
        StatFs var1 = new StatFs(var0.getPath());
        long var2;
        long var4;
        if (VERSION.SDK_INT < 18) {
            var2 = (long)var1.getBlockSize();
            var4 = (long)var1.getBlockCount();
        } else {
            var2 = var1.getBlockSizeLong();
            var4 = var1.getBlockCountLong();
        }

        return var4 * var2;
    }

    public static long getTotalSDExternalMemorySize(Context var0) {
        String var1 = getSDExternalPath(var0);
        return !var1.equals("unknown") ? getTotalMemorySize(new File(var1)) : -1L;
    }

    public static boolean isAvailableDataPath() {
        boolean var0 = false;

        boolean var10001;
        BufferedReader var2;
        try {
            String[] var1 = new String[]{"/system/bin/sh", "-c", "cat /proc/mounts"};
            var2 = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(var1).getInputStream()));
        } catch (IOException var8) {
            var10001 = false;
            return var0;
        }

        while(true) {
            String var4;
            try {
                var4 = var2.readLine();
            } catch (IOException var7) {
                var10001 = false;
                break;
            }

            if (var4 == null) {
                break;
            }

            boolean var5;
            try {
                var5 = var4.split(" ")[1].equals("/data");
            } catch (Exception var6) {
                var10001 = false;
                break;
            }

            if (var5) {
                var0 = true;
            }
        }

        return var0;
    }

    public static boolean isExternalStorageWritable() {
        return "mounted".equals(Environment.getExternalStorageState());
    }
}
